package com.hudson.plugin.server.hibernateDB.internal.domain.dao;

import com.hudson.plugin.server.hibernateDB.domain.dao.BuildRelationsDAOInterface;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.BuildRelationsEntity;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * BuildRelations entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .BuildRelations
 */

public class BuildRelationsDAO extends HibernateDaoSupport implements BuildRelationsDAOInterface {
    private static final Logger log = LoggerFactory
            .getLogger(BuildRelationsDAO.class);

    protected void initDao() {
        // do nothing
    }

    public void save(BuildRelationsEntity transientInstance) {
        log.debug("saving BuildRelations instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(BuildRelationsEntity persistentInstance) {
        log.debug("deleting BuildRelations instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public BuildRelationsEntity findById(Integer id) {
        log.debug("getting BuildRelations instance with id: " + id);
        try {
            BuildRelationsEntity instance = (BuildRelationsEntity) getHibernateTemplate()
                    .load(BuildRelationsEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(BuildRelationsEntity instance) {
        log.debug("finding BuildRelations instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding BuildRelations instance with property: "
                + propertyName + ", value: " + value);
        try {

            String queryString = "from BuildRelationsEntity as model where model." + propertyName + " = ?";
            List results = getHibernateTemplate().find(queryString, value);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByParentId(Object parentId) {
        return findByProperty(PARENT_ID, parentId);
    }

    public List findByChildId(Object childId) {
        return findByProperty(CHILD_ID, childId);
    }

    public List findAll() {
        log.debug("finding all BuildRelations instances");
        try {
            String queryString = "from BuildRelationsEntity";
            List results = getHibernateTemplate().find(queryString);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public BuildRelationsEntity merge(BuildRelationsEntity detachedInstance) {
        log.debug("merging BuildRelations instance");
        try {
            BuildRelationsEntity result = (BuildRelationsEntity) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(BuildRelationsEntity instance) {
        log.debug("attaching dirty BuildRelations instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(BuildRelationsEntity instance) {
        log.debug("attaching clean BuildRelations instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static BuildRelationsDAOInterface getFromApplicationContext(
            ApplicationContext ctx) {
        return (BuildRelationsDAOInterface) ctx.getBean("BuildRelationsDAO");
    }


    public List<Integer> findParentIdByChildId(Integer childId) {
        log.debug("finding ParentId By ChildId");
        try {
            Query query = this.getSession().createQuery(
                    "select r.parentId from BuildRelationsEntity r where r.childId = :child_id");
            query.setParameter("child_id", childId);
            List<Integer> childIds = query.list();
            if (childIds == null) {
                return new ArrayList();
            }
            return childIds;
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }

    }
}